<form action="?action=bind" methid="post" id="bind_weixin">
    <div class="form-group">
        <input type="text" class="form-control" name="student_no" placeholder="输入学号绑定">
    </div>
    <div class="form-group">
        <input type="text" class="form-control" name="idno" placeholder="输入身份证号码绑定">
    </div>
    <button type="submit" class="btn btn-success btn-block">绑定微信</button>
    <p class="text-center">或者</p>
</form>
<div class="text-center">
    <div class="register-name" style="margin-bottom: 20px;display: none">
        <p><img src="<?=$wx_user_info['headimgurl']?>"
            style="width: 120px;height: 120px;" class="img-thumbnail img-head img-circle"></p>
        <p><input type="text" class="form-control" id="nick_name" value="<?=$wx_user_info['nick_name']?>" placeholder="请输入您的昵称"></p>
    </div>
    <a href="?action=register" class="btn-register btn btn-primary btn-block">匿名投票</a>
</div>
<script>
    $(function () {
        $('.btn-register').on('click',function(){
            var btn = $(this);
            var name = $('#nick_name').val();
            if(name && $('div.register-name').is(':visible')){
                btn.attr('href','?action=register&user_name=' + encodeURI(name) + '&head_img=' + encodeURIComponent($('.img-head').attr('src')));
                return true;
            }else {
                if($('div.register-name').is(':visible')){
                    alert('请输入昵称后再继续操作');
                    return false;
                }
            }
            $('div.register-name').show();
            $('#bind_weixin').hide();
            btn.text('确定并开始投票');
            return false;
        });
        $('#bind_weixin').on('submit', function () {
            var frm = $(this);
            $.post(frm.attr('action'), frm.serializeArray(), function (ret) {
                if (ret['code'] == 0) {
                    location.href = '?action=done';
                } else {
                    alert(ret['message']);
                }
            }, 'json')
                .fail(function () {
                    alert('绑定微信数据失败');
                })
            return false;
        });
    })
</script>